<?php

function get_attachments_info($content = "")  {

	global $post, $wp_version;
	
	if (!is_single()) {
		return $content;}

	// get attachments
	$attachments = get_children( 
				array('post_parent' => $post->ID,
					'numberposts'	=> -1,
					'post_type'	=> 'attachment',
					'post_mime_type' => 'application/x-bittorrent',
					'orderby'	=> $order_by,
					'order'	=> $order
				)
			);
			
	// if no attachments, stop and exit
	if (!$attachments) {
		return $content;
	}
	
	// Start preparing the output
	$output = "\n<div class=\"wpbt\">\n";
	$output .= "<table class=\"wpbt_dtable\">";
	$output .= "<thead> <tr> <th align=\"left\">";

	
	// If a tagline is set, display it above the links list
	//	$tagline = get_option("wpbt_tagline");
	$tagline = "Download, Share and Enjoy:";
	
	if ($tagline != "") {
		$output .= stripslashes($tagline);
	}
	
	$output .= "</th>";
	$output .= "<th> Size </th>";
	$output .= '<th><img src="/wp-content/plugins/wpbtlite/images/arrowup.gif" alt="Up" title="Uploaders"></th>';
	$output .= '<th><img src="/wp-content/plugins/wpbtlite/images/arrowdown.gif" alt="Down" title="Downloaders"></th>';
	$output .= '<th><img src="/wp-content/plugins/wpbtlite/images/complete.gif" alt="Done" title="Completed"></th>';
	$output .= '<th><img src="/wp-content/plugins/wpbtlite/images/files.gif" alt="Files" title="Files"></th>';
	$output .= "</thead>";
	
	// Prepare attachment list
	foreach ( $attachments as $attachment ) {
	
	// Collect torrent information to display
	// File Name, Download Link, File Size, File No#, File List, Uploaders, Downloaders, Completed

	$file_link = wp_get_attachment_url($attachment->ID);
  	$file_name_array = explode("/",$file_link);
  	$file_name = array_reverse($file_name_array);
  	
  	// Get File metadata from DB
  	$tfile_metadata = wp_get_attachment_metadata($attachment->ID);
  	
  	// If DB is empty get data from file and update DB
  	if (!$tfile_metadata || $tfile_metadata['stime'] <= (time()-86400)) {
  	
//  	echo "no metadata found in db, getting live stats";
  	
  		$tfile = get_attached_file($attachment->ID);
  		
  		$dec = new File_Bittorrent2_Decode;
  		$tfile_info = $dec->decodeFile($tfile);
		
		// Decode the torrents current live stats
  		$scrape_info = $dec->getStats();
  		
  		if (!$scrape_info){
  		
  			$scrape_last = array ('stime' => time());
  			$scrape_info = array (
  							'complete' => 'N/A', 
  							'incomplete' => 'N/A',
  							'downloaded' => 'N/A' );
  			$scrape_info = $scrape_info + $scrape_last;
  		
  		}else{
  		
  			$scrape_last = array ('stime' => time());
  			$scrape_info = $scrape_info + $scrape_last;
  		}
		
		$tfile_metadata = $tfile_info + $scrape_info;

		wp_update_attachment_metadata($attachment->ID, $tfile_metadata);
  	
  	}

  	/**
	 * Start writing out torrent information to output
	 */

	  	$output .= '<tr> <td align=\"left\"> <a href="'. $file_link.'">Download This Torrent <img src="/wp-content/plugins/wpbtlite/images/download.gif" alt="Download" title="Download This Torrent"></a> </td>';
	  	
		// Loop through the file-list and display them in a table
		$files = $tfile_metadata['files'];
		$count = 0;

//	File list section
//		foreach ($files as $file) {
//		    $count++;
//			echo "<tr> <td align=\"left\">";
//		    echo  $file['filename'];
//			echo "</td> <td align=\"right\">";
//			echo mksize($file['size']);
//			 echo "</td> </tr>";
//			}
//		echo "</table>"; 	
  		
  		$output .= '<td>';
		$output .=  mksize($tfile_metadata['size']);
  		$output .= '</td> <td>';
		$output .=  $tfile_metadata['complete'];
  		$output .= '</td> <td>';
  		$output .=  $tfile_metadata['incomplete'];
  		$output .= '</td> <td>';
		$output .=  $tfile_metadata['downloaded'];
  		$output .= '</td> <td>';
		$output .=  count($tfile_metadata['files']);
  		$output .= '</tr> </td>';  	
	
	}
  	$output .= "</table>";
  	$output .= "</div>";
  
	return $content .= $output;
	
}

function mksize($bytes)
{
        if ($bytes < 1000 * 1024)
                return number_format($bytes / 1024, 0) . "kB";
        if ($bytes < 1000 * 1048576)
                return number_format($bytes / 1048576, 0) . "MB";
        if ($bytes < 1000 * 1073741824)
                return number_format($bytes / 1073741824, 0) . "GB";
        if ($bytes < 1000 * 1099511627776)
                return number_format($bytes / 1099511627776, 0) . "TB";
        if ($bytes < 1000 * 1125899906842620)
                return number_format($bytes / 1125899906842620, 0) . "PB";
        if ($bytes < 1000 * 1152921504606850000)
                return number_format($bytes / 1152921504606850000, 0) . "EB";
}



?>